<template>
  <div class="auth-page">
    <div class="auth-container">
      <!-- 顶部品牌区域 -->
      <div class="brand-header">
        <h1 class="brand-title">Quora</h1>
        <p class="brand-slogan">
          A place to share knowledge and better understand the world
        </p>
      </div>

      <!-- 主要内容区域 -->
      <div class="main-content">
        <!-- 左侧区域：服务条款和社交登录 -->
        <div class="left-section">
          <!-- 服务条款提示 -->
          <div class="terms-notice">
            By continuing you indicate that you agree to Quora's
            <a href="#" class="term-link">Terms of Service</a> and
            <a href="#" class="term-link">Privacy Policy</a>.
          </div>

          <!-- 社交媒体登录 -->
          <button class="social-btn google-btn">
            <span class="social-icon">G</span>
            Continue with Google
          </button>

          <button class="social-btn facebook-btn">
            <span class="social-icon">f</span>
            Continue with Facebook
          </button>

          <!-- 邮箱注册链接 -->
          <div class="email-signup">
            <!-- <router-link to="/register" class="email-signup-link"
              ></router-link
            > -->
            Sign up with email
          </div>
        </div>

        <!-- 右侧区域：登录表单 -->
        <div class="right-section">
          <h2 class="login-title">Login</h2>

          <form class="login-form">
            <!-- Email -->
            <div class="form-group">
              <label for="email">Email</label>
              <input
                id="email"
                v-model="loginForm.account"
                type="text"
                placeholder="Your email"
                class="form-input"
              />
            </div>

            <!-- 密码 -->
            <div class="form-group">
              <label for="password">Password</label>
              <input
                id="password"
                v-model="loginForm.password"
                type="password"
                placeholder="Your password"
                class="form-input"
              />
            </div>

            <!-- 登录按钮和忘记密码 -->
            <div class="login-actions">
              <a href="#" class="forgot-password">Forgot password?</a>
              <button
                type="button"
                class="login-btn"
                :disabled="loading"
                @click="handleLogin"
              >
                Login
              </button>
            </div>
          </form>
        </div>
      </div>

      <!-- 底部语言和链接 -->
      <div class="bottom-section">
        <!-- 语言链接 -->
        <div class="language-section">
          <span>New:</span>
          <a href="#" class="language-link">עברית</a>,
          <a href="#" class="language-link">العربية</a>,
          <a href="#" class="language-link">polski</a>,
          <a href="#" class="language-link">ગુજરાતી</a>,
          <a href="#" class="language-link">ಕನ್ನಡ</a>,
          <a href="#" class="language-link">മലയാളം</a> and
          <a href="#" class="language-link">ತುಳು</a>
        </div>

        <!-- 页脚链接 -->
        <div class="footer-links">
          <a href="#" class="footer-link">About</a>
          <span>·</span>
          <a href="#" class="footer-link">Careers</a>
          <span>·</span>
          <a href="#" class="footer-link">Privacy</a>
          <span>·</span>
          <a href="#" class="footer-link">Terms</a>
          <span>·</span>
          <a href="#" class="footer-link">Contact</a>
          <span>·</span>
          <a href="#" class="footer-link">Languages</a>
          <span>·</span>
          <a href="#" class="footer-link">Your Ad Choices</a>
          <span>·</span>
          <a href="#" class="footer-link">Press</a>
          <span>·</span>
          <span>© Quora, Inc. 2025</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/user";

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

// 表单引用
const loginFormRef = ref();

// 加载状态
const loading = ref(false);

// 登录表单数据
const loginForm = reactive({
  account: "",
  password: "",
});

// 表单验证规则
const loginRules = {
  account: [
    { required: true, message: "Please enter your email", trigger: "blur" },
    { min: 3, message: "Email must be at least 3 characters", trigger: "blur" },
  ],
  password: [
    { required: true, message: "Please enter your password", trigger: "blur" },
    {
      min: 6,
      message: "Password must be at least 6 characters",
      trigger: "blur",
    },
  ],
};

// 处理登录
const handleLogin = async () => {
  try {
    loading.value = true;

    // 简单的客户端验证
    if (!loginForm.account || !loginForm.password) {
      ElMessage.error("Please fill in all required fields");
      return;
    }

    // 调用登录接口
    const result = await userStore.loginAction({
      account: loginForm.account,
      password: loginForm.password,
    });

    if (result.success) {
      ElMessage.success("Login successful");

      // 跳转到原来的页面或首页
      const redirect = route.query.redirect || "/";
      router.push(redirect);
    } else {
      ElMessage.error(result.message || "Login failed");
    }
  } catch (error) {
    console.error("Login failed:", error);
    ElMessage.error("Login failed, please try again later");
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.auth-page {
  min-height: 100vh;
  background: url("/public/login_bg.webp") no-repeat center center fixed;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.auth-container {
  width: 100%;
  max-width: 720px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

/* 顶部品牌区域 */
.brand-header {
  text-align: center;
  margin-bottom: 40px;
}

.brand-title {
  font-size: 48px;
  font-weight: 700;
  color: #b92b27;
  margin: 0 0 12px 0;
}

.brand-slogan {
  font-size: 18px;
  color: #333;
  margin: 0;
  font-weight: 400;
}

/* 主要内容区域 */
.main-content {
  display: flex;
  justify-content: space-between;
  gap: 40px;
  margin-bottom: 30px;
}

/* 左侧区域 */
.left-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.terms-notice {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin-bottom: 24px;
}

.term-link {
  color: #0066ff;
  text-decoration: none;
}

.term-link:hover {
  text-decoration: underline;
}

.social-btn {
  width: 100%;
  height: 42px;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 12px;
  border: 1px solid #e0e0e0;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  font-family: inherit;
}

.social-btn:hover {
  background-color: #f5f5f5;
}

.social-icon {
  font-size: 18px;
  font-weight: bold;
}

.google-btn .social-icon {
  color: #4285f4;
}

.facebook-btn .social-icon {
  color: #3b5998;
}

.email-signup {
  color: #ccc;
  text-align: center;
  margin-top: 8px;
}

.email-signup-link {
  text-align: center;
  display: inline-block;
  color: #ccc;
  text-decoration: none;
  font-size: 16px;
  font-weight: 400;
}

.email-signup-link:hover {
  text-decoration: underline;
}

/* 右侧区域 */
.right-section {
  flex: 1;
  max-width: 320px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 24px 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  margin-bottom: 8px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #0066ff;
  box-shadow: 0 0 0 2px rgba(0, 102, 255, 0.1);
}

/* 已移除 form-options 类，使用 login-actions 代替 */

.forgot-password {
  color: #0066ff;
  font-size: 14px;
  text-decoration: none;
}

.forgot-password:hover {
  text-decoration: underline;
}

.login-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.login-btn {
  width: auto;
  padding: 0 24px;
  height: 36px;
  border-radius: 18px;
  font-size: 14px;
  font-weight: 500;
  background-color: #0066ff;
  border: none;
  color: white;
  cursor: pointer;
  font-family: inherit;
}

.login-btn:hover {
  background-color: #0066ff;
}

.login-btn:active {
  background-color: #0066ff;
}

.login-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* 底部区域 */
.bottom-section {
  border-top: 1px solid #eee;
  padding-top: 24px;
}

.language-section {
  font-size: 12px;
  color: #666;
  text-align: center;
  margin-bottom: 16px;
  line-height: 1.4;
}

.language-link {
  color: #0066ff;
  text-decoration: none;
  margin: 0 4px;
}

.language-link:hover {
  text-decoration: underline;
}

.footer-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #666;
}

.footer-link {
  color: #666;
  text-decoration: none;
}

.footer-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
    gap: 40px;
  }

  .right-section {
    max-width: 100%;
  }

  .brand-title {
    font-size: 36px;
  }

  .brand-slogan {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .auth-container {
    padding: 20px 0;
  }

  .brand-title {
    font-size: 28px;
  }

  .footer-links {
    font-size: 11px;
    gap: 6px;
  }
}
</style>
